<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Admin\Controllers;

use App\Admin\Actions\Row\AdvanceCardApproveAction;
use App\Admin\Actions\Row\AdvanceCardRejectAction;
use App\Admin\Repositories\AdvanceCard;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class AdvanceCardController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new AdvanceCard('user'), function (Grid $grid) {
            $grid->disableCreateButton();
            $grid->disableRefreshButton();
            $grid->disableViewButton();
            $grid->disableRowSelector();
            $grid->column('id')->sortable();
            $grid->column('user.name', '用户名');
            $grid->column('user.flag', '用户标签');
            $grid->column('user.telephone', '会员电话')->copyable();
            $grid->column('total_count');
            $grid->column('deposit_amount');
            $grid->column('remainder_count');
            $grid->column('remainder_amount');
            $grid->column('consume_beans');
            $grid->column('status', '状态')->sortable()->using([
                1 => '待审批',
                2 => '已启用',
                3 => '已驳回',
                4 => '已失效',
            ]);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                // 移除默认操作
                $actions->disableDelete();
                $actions->disableEdit();
                $actions->disableQuickEdit();
                $actions->disableView();

                // 仅当状态为待审核时显示按钮
                if ($actions->row->status === 1) {
                    $actions->append(new AdvanceCardApproveAction());
                    $actions->append(new AdvanceCardRejectAction());
                }
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('user.name', '用户名');
                $filter->equal('user.flag', '用户标签');
                $filter->equal('user.telephone', '用户电话');
                $filter->equal('status', '状态')->select([
                    1 => '待审批',
                    2 => '已启用',
                    3 => '已驳回',
                    4 => '已失效',
                ]);
                $filter->between('created_at', '创建时间')->datetime();
                $filter->between('updated_at', '修改时间')->datetime();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new AdvanceCard(), function (Show $show) {
            $show->field('status');
            $show->field('user_id');
            $show->field('total_count');
            $show->field('deposit_amount');
            $show->field('remainder_count');
            $show->field('remainder_amount');
            $show->field('consume_beans');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AdvanceCard(), function (Form $form) {
            $form->display('status');
            $form->text('user_id');
            $form->text('total_count');
            $form->text('deposit_amount');
            $form->text('remainder_count');
            $form->text('remainder_amount');
            $form->text('consume_beans');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
